package com.mccken.practice.top100.Top_1$20.Top_21;

import com.mccken.practice.top100.ListNode;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2024-01-24 13:58
 **/
public class ReverseList {
	public ListNode reverseList(ListNode head) {
		if (head == null || head.next == null){
			return head;
		}
		
		ListNode pre = null;
		ListNode curr = head;
		
		while (curr != null) {
			ListNode next = curr.next;
			curr.next = pre;
			pre = curr;
			curr = next;
		}
		return pre;
	}
	public ListNode reverseList1(ListNode head) {
		if (head == null || head.next == null){
			return head;
		}
		
		// 递归
		ListNode newHead = reverseList(head.next);
		head.next.next = head;
		head.next = null;
		return newHead;
	}
}
